﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace Ajax.Common
{
    public class FileIO
    {
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="FileString"></param>
        public void WriterFile(string FilePath, string FileString)
        {
            WriterFile(FilePath, FileString, Encoding.UTF8);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="FileString"></param>
        public void WriterFile(string FilePath, string FileString, Encoding encoding)
        {
            WriterFile(FilePath, FileString, false, encoding);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="FileString"></param>
        /// <param name="append"></param>
        public void WriterFile(string FilePath, string FileString, bool append)
        {
            WriterFile(FilePath, FileString, append, Encoding.UTF8);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="FileString"></param>
        /// <param name="append"></param>
        /// <param name="encoding"></param>
        public void WriterFile(string FilePath, string FileString, bool append, Encoding encoding)
        {
            string path = Path.GetDirectoryName(FilePath);
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            using (StreamWriter objWriter = new StreamWriter(FilePath, append, encoding))
            {
                objWriter.Write(FileString);
                objWriter.Flush();

            }
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="FilePath"></param>
        public string ReadFile(string FilePath)
        {
            return ReadFile(FilePath, Encoding.UTF8);
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="encoding"></param>
        public string ReadFile(string FilePath, Encoding encoding)
        {
            string OutPut;
            using (StreamReader fileStream = new StreamReader(FilePath, encoding))
            {
                OutPut = fileStream.ReadToEnd();

            }
            return OutPut;
        }

        /// <summary> 
        /// 复制目录及其文件往新目录
        /// </summary> 
        /// <param name="src">源目录</param> 
        /// <param name="dest">目标目录</param> 
        public static void CopyDirectory(string src, string dest)
        {
            DirectoryInfo di = new DirectoryInfo(src);
            foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
            {
                string destName = Path.Combine(dest, fsi.Name);
                if (fsi is FileInfo)
                    File.Copy(fsi.FullName, destName);
                else
                {
                    Directory.CreateDirectory(destName);
                    CopyDirectory(fsi.FullName, destName);
                }
            }
        }

        /// <summary>
        /// 递归获取文件夹大小
        /// </summary>
        /// <param name="path">文件夹物理路径</param>
        /// <returns></returns>
        public static long GetFilesSize(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            long size = 0;
            foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
            {
                if (fsi is FileInfo)
                {
                    size += ((FileInfo)fsi).Length;
                }
                else
                {
                    size += GetFilesSize(fsi.FullName);
                }
            }
            return size;
        }

        /// <summary>
        /// 转换文件大小的表达形式
        /// </summary>
        /// <param name="FileSize"></param>
        /// <returns></returns>
        public static string FileSize(double FileSize)
        {
            if (FileSize < 1024)
            {
                return string.Concat(FileSize, " B");
            }
            else if ((FileSize < 1048576))//1048576=1024 * 1024
            {

                return string.Concat(Math.Round((FileSize / 1024), 2), " KB");
            }
            else
            {

                return string.Concat(Math.Round(FileSize / 1048576, 2), " MB");
            }
        }

        /// <summary>
        /// 文件流编码
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="defaultEncoding"></param>
        /// <returns></returns>
        public Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)
        {
            Encoding bigEndianUnicode = defaultEncoding;
            if ((stream != null) && (stream.Length >= 2L))
            {
                byte num = 0;
                byte num2 = 0;
                byte num3 = 0;
                long offset = stream.Seek(0L, SeekOrigin.Begin);
                stream.Seek(0L, SeekOrigin.Begin);
                num = Convert.ToByte(stream.ReadByte());
                num2 = Convert.ToByte(stream.ReadByte());
                if (stream.Length >= 3L)
                {
                    num3 = Convert.ToByte(stream.ReadByte());
                }
                if (stream.Length >= 4L)
                {
                    Convert.ToByte(stream.ReadByte());
                }
                if ((num == 0xfe) && (num2 == 0xff))
                {
                    bigEndianUnicode = Encoding.BigEndianUnicode;
                }
                if (((num == 0xff) && (num2 == 0xfe)) && (num3 != 0xff))
                {
                    bigEndianUnicode = Encoding.Unicode;
                }
                if (((num == 0xef) && (num2 == 0xbb)) && (num3 == 0xbf))
                {
                    bigEndianUnicode = Encoding.UTF8;
                }
                stream.Seek(offset, SeekOrigin.Begin);
            }
            return bigEndianUnicode;
        }
    }
}